import logging
from common.db_tools import exe_sql
from config import BASE_HOST
import requests


class RegLoginApi:

    def __init__(self, session):
        self.ses = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        resp = self.ses.get(url=BASE_HOST + "/common/public/verifycode1/{}".format(r))
        logging.info("获取图片验证码接口的响应状态码{}".format(resp.status_code))
        return resp

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone_v, imgcode_v, type_v="reg"):
        resp = self.ses.post(url=BASE_HOST + "/member/public/sendSms",
                             data={"phone": phone_v, "imgVerifyCode": imgcode_v, "type": type_v})
        print("获取图片验证码响应体数据：", resp.json())
        logging.info("获取图片验证码响应体数据：{}".format(resp.json()))
        return resp

    # 注册接口
    def user_register(self, form_dict):
        resp = self.ses.post(url=BASE_HOST + "/member/public/reg",
                             data=form_dict)
        # logging.info("注册接口响应体数据：", resp.json())
        print("注册接口响应体数据：", resp.json())
        logging.info("注册接口响应体数据：{}".format(resp.json()))
        return resp

    # 登录接口
    def user_login(self, username_v, pas_v):
        resp = self.ses.post(url=BASE_HOST + "/member/public/login",
                             data={"keywords": username_v, "password": pas_v})
        # logging.info("登录接口响应体数据：", resp.json())
        print("登录接口响应体数据：", resp.json())
        logging.info("登录接口响应体数据：{}".format(resp.json()))
        return resp


if __name__ == '__main__':
    sql1 = "delete i.* from mb_member_info i inner join mb_member m on m.id = i.member_id where m.phone in ('13166661116');"
    sql2 = "delete l.* from mb_member_login_log l inner join mb_member m on m.id=l.member_id where m.phone in ('13166661116')"
    sql3 = "delete from mb_member where phone in ('13166661116')"
    sql4 = "delete from mb_member_register_log where phone in ('13166661116')"
    # 调用封装操作数据库的方法，执行SQL语句
    exe_sql(sql1, "czbk_member")
    exe_sql(sql2, "czbk_member")
    exe_sql(sql3, "czbk_member")
    exe_sql(sql4, "czbk_member")

    ses = requests.session()
    aa = RegLoginApi(ses)
    aa.get_img_verify_code(12356677)
    aa.get_phone_verify_code(13166661116, 8888)
    form_dict = {"phone": "13166661116", "password": "qq1234", "verifycode": "8888", "phone_code": "666666",
                 "dy_server": "on"}
    aa.user_register(form_dict)
    aa.user_login("13166661116", "qq1234")


